package com.zb_study.houserent.server;

import com.sun.scenario.effect.impl.prism.PrImage;
import com.zb_study.houserent.domain.House;

public class HouseService {
    private House[] houses;
    private int houseNum = 1;
    private int idCounter = 1;

    public HouseService(int size) {
        houses = new House[size];
        houses[0] = new House(1, "jack", "122", "富强区", 2000, "未出租");
    }


    public House findById(int findId) {
        for (int i = 0; i < houseNum; i++) {
            if (findId == houses[i].getId()) {
                return houses[i];
            }
        }
        return null;
    }


    public boolean del(int delId) {
        int index = -1;
        for (int i = 0; i < houseNum; i++) {
            if (delId == houses[i].getId()) {//判断房屋号是否存在
                index = i;
            }
        }
        if (index == -1) {
            return false;
        }

        for (int i = index; i < houseNum - 1; i++) {
            houses[i] = houses[i + 1];
        }
        houses[--houseNum] = null;
        return true;
    }


    public boolean add(House newHouse) {
        if (houseNum == houses.length) {
            System.out.println("数组已满,不能再添加了...");
            return false;
        }
        houses[houseNum++] = newHouse;
        newHouse.setId(++idCounter);
        return true;
    }

    public House[] list() {
        return houses;
    }
}
